/**  定义唇妆相关的接口 */
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const Response = require("../utils/Response.js");
const cors = require("cors");

// 引入mysql连接池
const pool = require("../utils/db.js");

router.use(cors());
router.use(express.static("public"));
// 查询
router.get("/star/leixing", (req, res) => {
  var obj = req.query;

  pool.query(
    "select * from Mac_star where leixing=?",
    [obj.leixing],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
      if (r.length === 0) {
        res.send({
          code: 400,
          msg: "查询失败",
        });
      } else {
        // 接口规范，返回的结果是对象，包含有状态码（人为规定），消息说明，如果需要返回数据，添加查询数据
        res.send({
          code: 200,
          msg: "查询成功",
          data: r,
        });
      }
    }
  );
  // query属于异步操作，需要 把响应的结果放入到回调函数的内部
});
// 执行模糊查询
router.post("/star/product_name", (req, resp) => {
  let { product_name } = req.body;
  //TODO 服务端表单验证  如果验证通过那么继续后续业务  如果验证不通过，则直接返回参数异常
  let schema = Joi.object({
    product_name: Joi.string().required(), // 必填
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 执行模糊查询
  let sql = "select * from Mac_star where product_name like ?";
  pool.query(sql, [`%${product_name}%`], (err, result) => {
    if (err) {
      resp.send(Response.error(500, error));
      throw err;
    } else {
      resp.send({
        code: 200,
        msg: "查询成功",
        data: result,
      });
    }
  });
});

// 模糊查询
router.post("/star/leixing_product_name", (req, resp) => {
  let { product_name, leixing } = req.body;
  //TODO 服务端表单验证  如果验证通过那么继续后续业务  如果验证不通过，则直接返回参数异常
  let schema = Joi.object({
    product_name: Joi.string().required(), // 必填
    leixing: Joi.string().required(), // 必填
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 执行模糊查询
  let sql =
    "select * from Mac_star where product_name like ? and leixing like ?";
  pool.query(sql, [`%${product_name}%`, `${leixing}`], (err, result) => {
    if (err) {
      resp.send(Response.error(500, error));
      throw err;
    } else {
      resp.send({
        code: 200,
        msg: "查询成功",
        data: result,
      });
    }
  });
});
// 查询
router.get("/star/query", (req, res) => {
  var obj = req.query;

  pool.query("select * from Mac_star where id=?", [obj.id], (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);
    if (r.length === 0) {
      res.send({
        code: 400,
        msg: "查询失败",
      });
    } else {
      // 接口规范，返回的结果是对象，包含有状态码（人为规定），消息说明，如果需要返回数据，添加查询数据
      res.send({
        code: 200,
        msg: "查询成功",
        data: r,
      });
    }
  });
  // query属于异步操作，需要 把响应的结果放入到回调函数的内部
});

// 查询所有
router.get("/star", (req, res) => {
  var obj = req.query;
  console.log(obj);

  pool.query("select * from Mac_star", [obj], (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);
    if (r.length === 0) {
      res.send({
        code: 400,
        msg: "查询失败",
      });
    } else {
      // 接口规范，返回的结果是对象，包含有状态码（人为规定），消息说明，如果需要返回数据，添加查询数据
      res.send({
        code: 200,
        msg: "查询成功",
        data: r,
      });
    }
  });
  // query属于异步操作，需要 把响应的结果放入到回调函数的内部
});

// 添加

router.use(
  express.urlencoded({
    extended: true,
  })
);
router.post("/star/add", (req, res) => {
  var {
    id,
    img,
    shade_name,
    product_name,
    product_rgn_name,
    product_brief_description,
    product_price,
    leixing,
  } = req.body;
  let sql =
    "insert into Mac_star (id,img,shade_name,product_name,product_rgn_name,product_brief_description,product_price,leixing) values (?,?,?,?,?,?,?,?)";
  pool.query(
    sql,
    [
      id,
      img,
      shade_name,
      product_name,
      product_rgn_name,
      product_brief_description,
      product_price,
      leixing,
    ],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
    }
  );
  res.send({
    code: 200,
    msg: "添加成功",
  });
});
// 删除
router.post("/star/del", (req, resp) => {
  let { id } = req.body;
  // 执行删除业务
  let sql = "delete from Mac_star where id = ?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      throw err;
    }
    if (result.affectedRows === 0) {
      resp.send({
        code: 400,
        msg: "删除失败",
      });
    } else {
      resp.send({
        code: 200,
        msg: "删除成功",
        data: result,
      });
    }
  });
});

// 修改
router.post("/star/put", (req, resp) => {
  let {
    id,
    img,
    shade_name,
    product_name,
    product_rgn_name,
    product_brief_description,
    product_price,
    leixing,
  } = req.body; // post请求参数在req.body中

  let sql =
    "update Mac_star set img=?,shade_name=?,product_name=?,product_rgn_name=?,product_brief_description=?,product_price=?,leixing=? where id=?";
  pool.query(
    sql,
    [
      img,
      shade_name,
      product_name,
      product_rgn_name,
      product_brief_description,
      product_price,
      leixing,
      id,
    ],
    (error, result) => {
      if (result.changedRows === 0) {
        resp.send({
          code: 400,
          msg: "修改失败",
        });
      } else {
        resp.send({
          code: 200,
          msg: "修改成功",
          data: result,
        });
      }
    }
  );
});

module.exports = router;
